/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import java.util.Vector;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author efraeli
 */
public class ButtonController extends Object {
    private Vector buttons;
    private int cursorPosition;
    private boolean visible;
    
    public ButtonController() {
        buttons = new Vector();
        cursorPosition=0;
        visible=false;
    }

    public void addButton(ButtonInterface button) {
        buttons.addElement(button);
        if(buttons.size()==1) {
            ButtonInterface currButton = (ButtonInterface) buttons.elementAt(0);
            currButton.hover();
        }
    }

    public void paint(Graphics g) {
        if(!visible)
            return;
        
        for(int i=0; i<buttons.size(); i++) {
           ButtonInterface button = (ButtonInterface)buttons.elementAt(i);
           button.paint(g);
        }
    }

    public void moveNext() {
        ButtonInterface prevButton = (ButtonInterface) buttons.elementAt(cursorPosition);
        prevButton.idle();

        cursorPosition=(cursorPosition+1)%buttons.size();
        
        ButtonInterface currButton = (ButtonInterface) buttons.elementAt(cursorPosition);
        currButton.hover();
    }

    public void movePrev() {
        ButtonInterface prevButton = (ButtonInterface) buttons.elementAt(cursorPosition);
        prevButton.idle();
        
        if(cursorPosition==0) {
            cursorPosition = buttons.size()-1;
        } else{
            cursorPosition=(cursorPosition-1)%buttons.size();
        }

        ButtonInterface currButton = (ButtonInterface) buttons.elementAt(cursorPosition);
        currButton.hover();
    }

    public int selected() {
        ButtonInterface currButton = (ButtonInterface) buttons.elementAt(cursorPosition);
        return currButton.selected();
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    public boolean isVisible() {
        return visible;
    }

}
